const { list } = require('./mock')

module.exports = app => {

	app.get('/api/list', (req, res) => {
		const { pageCode = 1, pageSize = 10 } = req.query;

		if (pageCode * pageSize <= list.length) {
			res.send({
				code: 200,
				data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
				message: '数据请求成功'
			})
		} else {
			res.send({
				code: 400,
				message: '没有更多数据了！'
			})
		}

	})

	app.post('/api/list/:id', (req, res) => {
		// 1. 获取动态路由参数
		const { id } = req.params;

		// 2. 接受 json
		// const { id } = req.body

		// 3. 接受查询字符串数据
		// const { id } = req.query
		const item = list.find(v => v.id === id)
		res.send({
			code: 200,
			data: item,
			message: '数据请求成功'
		})
	})
}